package com.houseService.users.controller;

import com.houseService.common.annotation.Log;
import com.houseService.common.core.controller.BaseController;
import com.houseService.common.core.domain.AjaxResult;
import com.houseService.common.enums.BusinessType;
import com.houseService.common.utils.poi.ExcelUtil;
import com.houseService.users.vo.UsersVo;
import com.houseService.users.service.IUsersService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import com.houseService.common.core.page.TableDataInfo;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * 用户Controller
 *
 * @author LiuChunlin
 * @date 2025-03-18
 */
@Slf4j
@Api(tags = "用户Controller")
@RestController
@RequestMapping("/users/users")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class UsersController extends BaseController {

    private final IUsersService usersService;

    @ApiOperation("查询用户列表")
    @PreAuthorize("@ss.hasPermi('users:users:list')")
    @GetMapping("/list")
    public TableDataInfo<UsersVo> list(UsersVo entity) {
        return usersService.queryList(entity);
    }

    @ApiOperation("查询用户所有列表")
    @GetMapping("/listAll")
    public AjaxResult listAll(UsersVo entity) {
        return AjaxResult.success("查询成功", usersService.queryAll(entity));
    }

    @ApiOperation("导出用户列表")
    @PreAuthorize("@ss.hasPermi('users:users:export')")
    @Log(title = "用户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, UsersVo entity) {
        List<UsersVo> list = usersService.queryAll(entity);
        ExcelUtil<UsersVo> util = new ExcelUtil<>(UsersVo.class);
        util.exportExcel(response, list, "用户数据");
    }

    @ApiOperation("获取用户详细信息")
    @PreAuthorize("@ss.hasPermi('users:users:query')")
    @GetMapping(value = "/getInfo/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success("查询成功", usersService.queryById(id));
    }

    @ApiOperation("新增用户")
    @PreAuthorize("@ss.hasPermi('users:users:add')")
    @Log(title = "用户", businessType = BusinessType.INSERT)
    @PostMapping("add")
    public AjaxResult add(@RequestBody UsersVo entity) {
        return toAjax(usersService.insertUsers(entity));
    }

    @ApiOperation("修改用户")
    @PreAuthorize("@ss.hasPermi('users:users:edit')")
    @Log(title = "用户", businessType = BusinessType.UPDATE)
    @PostMapping("edit")
    public AjaxResult edit(@RequestBody UsersVo entity) {
        return toAjax(usersService.updateUsersId(entity));
    }

    @ApiOperation("删除用户")
    @PreAuthorize("@ss.hasPermi('users:users:remove')")
    @Log(title = "用户", businessType = BusinessType.DELETE)
	@GetMapping("/remove/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(usersService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
